﻿using Newtonsoft.Json;
using Omabar.BLL;
using Omabar.Common;
using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace Omabar.CMS.Handler
{
    /// <summary>
    /// RouteFlightAdd 的摘要说明
    /// </summary>
    public class RouteFlightAdd : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            RESP_Info info = new RESP_Info();
            try
            {
                context.Response.ContentType = "text/plain";
                string ROUTE_LUGGAGE_CD = context.Request["ROUTE_LUGGAGE_CD"];
                string ROUTE_TAKEOFF_CD = context.Request["ROUTE_TAKEOFF_CD"];
                string ROUTE_LANDING_CD = context.Request["ROUTE_LANDING_CD"];
                string ROUTE_LUGGAGE_BJ = context.Request["ROUTE_LUGGAGE_BJ"];
                string ROUTE_TAKEOFF_BJ = context.Request["ROUTE_TAKEOFF_BJ"];
                string ROUTE_LANDING_BJ = context.Request["ROUTE_LANDING_BJ"];
                string ROUTE_ELSE = context.Request["ROUTE_ELSE"];
                string ROUTE_ID = context.Request["ROUTE_ID"];

                RouteFlight_BLL bll = new RouteFlight_BLL();
                ROUTEFLIGHT rf = bll.GetModel(ROUTE_ID);
                if (rf == null)
                {
                    rf = new ROUTEFLIGHT()
                    {
                        ROUTE_LUGGAGE_CD = string.IsNullOrEmpty(ROUTE_LUGGAGE_CD) ? "未知" : ROUTE_LUGGAGE_CD,
                        ROUTE_TAKEOFF_CD = string.IsNullOrEmpty(ROUTE_LUGGAGE_CD) ? "未知" : ROUTE_TAKEOFF_CD,
                        ROUTE_LANDING_CD = string.IsNullOrEmpty(ROUTE_LUGGAGE_CD) ? "未知" : ROUTE_LANDING_CD,
                        ROUTE_LUGGAGE_BJ = string.IsNullOrEmpty(ROUTE_LUGGAGE_CD) ? "未知" : ROUTE_LUGGAGE_BJ,
                        ROUTE_TAKEOFF_BJ = string.IsNullOrEmpty(ROUTE_LUGGAGE_CD) ? "未知" : ROUTE_TAKEOFF_BJ,
                        ROUTE_LANDING_BJ = string.IsNullOrEmpty(ROUTE_LUGGAGE_CD) ? "未知" : ROUTE_LANDING_BJ,
                        ROUTE_ELSE = string.IsNullOrEmpty(ROUTE_LUGGAGE_CD) ? "未知" : ROUTE_ELSE,
                        ROUTE_ID = ROUTE_ID,
                    };

                    info = bll.Add(rf);
                }
                else
                {
                    rf = new ROUTEFLIGHT()
                    {
                        ROUTE_LUGGAGE_CD = string.IsNullOrEmpty(ROUTE_LUGGAGE_CD) ? "未知" : ROUTE_LUGGAGE_CD,
                        ROUTE_TAKEOFF_CD = string.IsNullOrEmpty(ROUTE_LUGGAGE_CD) ? "未知" : ROUTE_TAKEOFF_CD,
                        ROUTE_LANDING_CD = string.IsNullOrEmpty(ROUTE_LUGGAGE_CD) ? "未知" : ROUTE_LANDING_CD,
                        ROUTE_LUGGAGE_BJ = string.IsNullOrEmpty(ROUTE_LUGGAGE_CD) ? "未知" : ROUTE_LUGGAGE_BJ,
                        ROUTE_TAKEOFF_BJ = string.IsNullOrEmpty(ROUTE_LUGGAGE_CD) ? "未知" : ROUTE_TAKEOFF_BJ,
                        ROUTE_LANDING_BJ = string.IsNullOrEmpty(ROUTE_LUGGAGE_CD) ? "未知" : ROUTE_LANDING_BJ,
                        ROUTE_ELSE = string.IsNullOrEmpty(ROUTE_LUGGAGE_CD) ? "未知" : ROUTE_ELSE,
                        ROUTE_ID = ROUTE_ID,
                    };

                    info = bll.Update(rf);
                }
            }
            catch (Exception ex)
            {
                Tom.Net.Log.LogManager.Error("添加路线航班失败", ex);
                info.Success = false;
                info.Title = "错误";
                info.Tip = "<h5><p>添加路线航班失败</p></h5>";
                info.Url = "RouteFlightAdd.aspx?id=" + context.Request["ROUTE_ID"];
            }

            context.Response.Write(JsonConvert.SerializeObject(info));
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}